package yana.tpe.servlets.task5.domain.dao.impl;

import java.sql.SQLException;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.Order;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.stereotype.Repository;
import yana.tpe.servlets.task5.domain.dao.UserDao;
import yana.tpe.servlets.task5.domain.domains.User;

@Repository
public class UserDaoImpl extends AbstracDaoImpl<Integer, User> implements UserDao {

    @Override
    public List<User> getOrderedList(final String parameter, final boolean ascending) {
        return hibernateTemplate.execute(new HibernateCallback<List<User>>() {

            @Override
            public List<User> doInHibernate(Session session)
                    throws HibernateException, SQLException {
                Criteria crit = session.createCriteria(getDomainClass());
                crit.addOrder(ascending ? Order.asc(parameter) : Order.desc(parameter));
                return crit.list();
            }
        });
    }

    @Override
    protected Class<User> getDomainClass() {
        return User.class;
    }
}
